package com.xly.webclick.cusview.adjust;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by xuleyuan on 23/11/2017
 */

public class AdjustConstraintLayout extends ConstraintLayout {

    public AdjustConstraintLayout(Context context) {
        super(context);
    }

    public AdjustConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AdjustConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        AdjustUtils.adjustViewPadding(this);
    }


    @Override
    public void addView(View child) {
        super.addView(child);
    }

    @Override
    public void onViewAdded(View view) {
        super.onViewAdded(adjust(view));
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        if (params.height > 0)
            params.height = KeepDesignValueUtils.getInstance().calculatorValue(params.height);
        if (params.width > 0)
            params.width = KeepDesignValueUtils.getInstance().calculatorValue(params.width);
        super.setLayoutParams(params);
    }


    public View adjust(View view) {
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                adjust(((ViewGroup) view).getChildAt(i));
            }
            view.setLayoutParams(AdjustUtils.commonAdjust(view.getLayoutParams()));
            AdjustUtils.adjustView(view);
            return view;
        } else {
            view.setLayoutParams(AdjustUtils.commonAdjust(view.getLayoutParams()));
            AdjustUtils.adjustView(view);
            return view;
        }
    }

}
